package Presentation;

import java.awt.Dimension;

import javax.swing.JPanel;

import solitaire.application.Usine;

import Controlor.CTasDeCarteAlternee;
import Controlor.CTasDeCartes;

public class PTasDeCartes extends JPanel{

	private CTasDeCartes controller;
	public static int HORIZONTAL=0;
	public static int HORIZONTALx3=1;
	public static int VERTICAL=2;
	public static int NON=3;
	protected int align;
	
	public static int GAP = 15;
	
	public PTasDeCartes(CTasDeCartes c,Usine u){
		controller = c;
		setLayout(null);
		if(controller != null) {
			this.computeDimension();
		}
	}


	public void depiler(PCarte presentation) {
		remove(presentation);
		repaint();
		computeDimension();
	}

	private void computeDimension() {
		int width = 0;
		int height = 0;
		if(controller.getNombre()==0||controller==null){
			width = PCarte.WIDTH;
			height = PCarte.HEIGHT;
			if(align == HORIZONTAL){
				width = PCarte.WIDTH;
				height = PCarte.HEIGHT+((controller.getNombre()-1)*GAP);
			}else if(align == VERTICAL){
				width = PCarte.WIDTH + ((controller.getNombre()-1)*GAP);
				height = PCarte.HEIGHT;
			}else if(align == HORIZONTALx3){
				width = PCarte.WIDTH + GAP * 2;
				height = PCarte.HEIGHT;
			}else if(align == NON){
				width = PCarte.WIDTH;
				height = PCarte.HEIGHT;
			}
		}
		setSize(width, height);		
		setMinimumSize(new Dimension(width,height));
		setPreferredSize(new Dimension(width,height));
	}

	public void empiler(PCarte pCarte) {
		add(pCarte,0);
		if(align == VERTICAL){
			if(controller.getNombre() == 1)
				pCarte.setLocation(0, 0);
			else
				pCarte.setLocation(0, getHeight() - PCarte.HEIGHT + GAP);
			
		} else if (align == HORIZONTAL) {
			if(controller.getNombre() == 1){
				pCarte.setLocation(0, 0);
			}
			else 
				pCarte.setLocation(getWidth() - PCarte.WIDTH + GAP, 0);
			
		} else if (align == HORIZONTALx3) {
			int index = controller.getNombre()-1;
			switch (index % 3){
				case 0: pCarte.setLocation(0, 0); break;
				case 1: pCarte.setLocation(GAP, 0); break;
				case 2: pCarte.setLocation(GAP * 2, 0); break;
				default: break;
			}
		} else {
			pCarte.setLocation(0, 0);
		}
		
		computeDimension();
		
	}

}
